package cn.org.timespace.easyframe.util.tag;

import java.util.List;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.util.dictionary.EfDictionary;

/**
 * 生成下拉列表框
 * 
 * @author wubo
 * 
 */
public class SelectOptionTag extends BodyTagSupport {

	private static final Log log = LogFactory.getLog(SelectOptionTag.class);

	private static final long serialVersionUID = -7054926525472151702L;

	private static enum Type {
		table, // 直接从字典表里面获取数据
		sql     //通过字典表里面配置的sql语句获取数据
	}

	public SelectOptionTag() {
		super();
	}

	// 类型 不是必须的 默认为静态的
	private String type;

	// 分组名 必须的
	private String groupname;

	// 需选中的值 不是必须的
	private String selectedValue;

	// 是否加上 --请选择-- 不是必须的 默认不加 --请选择--
	private boolean flag;

	// 是否显示value 不是必须的 默认不显示value 只显示name
	private boolean link;
	
	/**
	 * SQL条件过滤
	 * 
	 * 如果 type="SQL" 时，动态添加SQL的where条件
	 */
	private String where;
	
	/**
	 * 过滤条件
	 * 如果 type="table"时，根据数据库的filter值进行过滤。支持的表达式有 > < >= <=
	 */
	private String filter;

	public int doStartTag() {
		return SKIP_BODY;
	}

	public int doEndTag() throws JspTagException {
		try {

			if (type == null) {
				type = Type.table.toString(); // 默认从字典表中直接获取
			}

			List<EfDictionary> dicList = null;
			if (Type.table.toString().equalsIgnoreCase(type)) {
				if (groupname != null && groupname.trim().length() > 0) {
					dicList = Translate.getInstance().getDataListByTable(groupname,filter);
				}
			} else if (Type.sql.toString().equalsIgnoreCase(type)) {
				if (groupname != null && groupname.trim().length() > 0) {
					dicList = Translate.getInstance().getDataListBySql(groupname,where);
				}
			}

			// 取出备注 该内容追加到"--请选择--"后面
			String remark = "";
			if (dicList != null && dicList.size() > 0) {
				remark = dicList.get(0).getRemark();
			}

			if (flag) {
				pageContext.getOut().println(
						"<option value=''>--请选择" + remark + "--</option>");
			}

			if (dicList != null) {
				for (EfDictionary dic : dicList) {
					String itemValue = dic.getItemvalue();
					String itemName = dic.getItemname();

					String isSelected = "";
					if (selectedValue != null
							&& selectedValue.equals(itemValue)) {
						isSelected = "selected";
					}

					String display = "";
					if (link) {
						display = itemValue + " -- " + itemName;
					} else {
						display = itemName;
					}

					pageContext.getOut().println(
							"<option value='" + itemValue + "' " + isSelected
									+ ">" + display + "</option>");
				}
			}

		} catch (Exception ex) {
			if (log.isErrorEnabled())
				log.error("根据<" + getGroupname() + ">生成下拉框出错，原因是："
						+ ex.getMessage());
			ex.printStackTrace();
		}

		return EVAL_PAGE;
	}

	public boolean isFlag() {

		return flag;
	}

	public String getType() {
		return type;
	}

	public boolean isLink() {
		return link;
	}

	public void setFlag(boolean flag) {

		this.flag = flag;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setLink(boolean link) {
		this.link = link;
	}

	public String getGroupname() {
		return groupname;
	}

	public void setGroupname(String groupname) {
		this.groupname = groupname;
	}
	
	public String getSelectedValue() {
		return selectedValue;
	}

	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}

	public void release() {
		super.release();
	}

	public String getWhere() {
		return where;
	}

	public void setWhere(String where) {
		this.where = where;
	}

	public String getFilter() {
		return filter;
	}

	public void setFilter(String filter) {
		this.filter = filter;
	}
	
}
